export * from './query';
import Taro from '@tarojs/taro';
/**
 * 验证手机号
 */
export const isMobile = (mobile) => {
    let result = true;
    if (!mobile) {
        Taro.showToast({
            title: '请输入手机号',
            icon: "none",
            duration: 2000
        })
        result = false;
    }
    const Reg = /^1\d{10}$/;
    if (!Reg.test(mobile)) {
        Taro.showToast({
            title: '请输入正确手机号格式',
            icon: "none",
            duration: 2000
        })
        result = false;
    }
    return result;
}

/**
 * 根据locale格式化金额
 *
 * @export
 * @param {number} [number=0] 需格式化的金额
 * @returns
 */
export const formatCurrency = function (number) {
    let beforePoint = '',
        afterPoint = '',
        newArr = [],
        beforeNumber = '';

    let intnumber = +number;

    let text = parseFloat(intnumber);

    if (text < 0) {
        beforeNumber = '-';
        text = Math.abs(text);
    }

    text += '';

    if (text.indexOf('.') != -1) {
        text = parseFloat(text).toFixed(3) + '';
        text = text.substring(0, text.lastIndexOf('.') + 3);
        let arr = text.split('.');
        beforePoint = arr[0];
        afterPoint = arr[1] == '00' ? '' : ('.' + arr[1]);
    } else {
        beforePoint = text;
    }

    let specialArr = beforePoint.split('');
    specialArr.reverse();
    let len = specialArr.length;

    for (let i = 1; i <= len; i++) {
        newArr.push(specialArr[i - 1]);
        if (i % 3 == 0 && i != 0 && i != len) {
            newArr.push(',');
        }
    }
    beforePoint = newArr.reverse();
    beforePoint = beforePoint.join('');
    return beforeNumber + beforePoint + afterPoint;
};

/**
 * 页面跳转
 */
export const go = (url) => {
    // browserHistory.push('/my');
    window.location.href = '/' + url;
    // window.history.pushState(null, null, url);
}
/**
 * 判断是否已登录
 */
export const isLogin = () => {
    return !!localStorage.getItem('token');
}

export const showToast = (text) => {
    Taro.showToast({
        title: text,
        icon: 'none',
    });
}
const dateFormators = {
    __proto__: null,
    y: (date, format) => {
        const year = date.getFullYear()
        return format.length < 3 ? year % 100 : year
    },
    M: (date) => date.getMonth() + 1,
    d: (date) => date.getDate(),
    H: (date) => date.getHours(),
    m: (date) => date.getMinutes(),
    s: (date) => date.getSeconds(),
    e: (date) => "日一二三四五六".charAt(date.getDay())
}

/**
 * 格式化日期对象
 * @param date 日期对象
 * @param format 格式字符串，其中以下字符（区分大小写）会被替换：
 *
 * 字符| 意义          | 示例
 * ----|--------------|--------------------
 * y   | 年           | yyyy: 1999, yy: 99
 * M   | 月           | MM: 09, M: 9
 * d   | 日           | dd: 09, d: 9
 * H   | 时（24小时制）| HH: 13, H: 13
 * m   | 分           | mm: 06, m: 6
 * s   | 秒           | ss: 06, s: 6
 * e   | 周（中文）    | 周e: 周一
 *
 * @return 返回格式化后的字符串
 * @example formatDate(new Date("2016/01/01 00:00:00")) // "2016/01/01 00:00:00"
 * @example formatDate(new Date("2016/01/01 00:00:00"), "yyyyMMdd") // "20160101"
 * @see https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
 */
export const formatDate = (date = new Date(), format = "yyyy/MM/dd HH:mm:ss e") => {
    if (date && !(date instanceof Date)) {
        date = new Date(date)
    }
    if (!+date) {
        return ""
    }
    return format.replace(/([yMdHmse])\1*/g, (all, key) => {
        key = (dateFormators)[key](date, all) + ""
        while (key.length < all.length) {
            key = "0" + key
        }
        return key
    })
}
/**
 * 获取小程序路由传参
 */
export const getParams = () => {
    const currentPages = Taro.getCurrentPages();
    const options = currentPages[currentPages.length - 1].options;
    return options;
}